use dataset,clear
set more off
//remove case with all missing values
drop if case==312217335 & partyorder==.

//gen PID
encode CLOSEPTY2, gen(party)
tab party, gen (party_)	   
recode party (42 43 44 45 23 2 =1)
recode party (4 6 9 11 18 20 25 26 28 30 34 =1)
recode party (1=.)

//on immigration issues, unifying missing
replace C1_3_a = "NA" if C1_3_a=="[998]Skipped" | C1_3_a=="[999]Not Asked"
replace C1_3_b = "NA" if C1_3_b=="[998]Skipped" | C1_3_b=="[999]Not Asked"
replace C1_3_c = "NA" if C1_3_c=="[998]Skipped" | C1_3_c=="[999]Not Asked"
replace C1_3_d = "NA" if C1_3_d=="[998]Skipped" | C1_3_d=="[999]Not Asked"
replace C1_3_e = "NA" if C1_3_e=="[998]Skipped" | C1_3_e=="[999]Not Asked"
replace C1_3_f = "NA" if C1_3_f=="[998]Skipped" | C1_3_f=="[999]Not Asked"
replace C1_3_g = "NA" if C1_3_g=="[998]Skipped" | C1_3_g=="[999]Not Asked"
replace C1_3_h = "NA" if C1_3_h=="[998]Skipped" | C1_3_h=="[999]Not Asked"
replace C1_3_i = "NA" if C1_3_i=="[998]Skipped" | C1_3_i=="[999]Not Asked"
replace C3_1a = "NA" if C3_1a=="[8]Skipped" | C3_1a=="Don't Know"
replace C3_1b_1 = "NA" if C3_1b_1=="[8]Skipped" | C3_1b_1=="Don't Know"
replace C3_1b_2 = "NA" if C3_1b_2=="[8]Skipped" | C3_1b_2=="Don't Know"
replace C3_1b_3 = "NA" if C3_1b_3=="[8]Skipped" | C3_1b_3=="Don't Know"
replace C3_1b_4 = "NA" if C3_1b_4=="[8]Skipped" | C3_1b_4=="Don't Know"
replace C3_1b_5 = "NA" if C3_1b_5=="[8]Skipped" | C3_1b_5=="Don't Know"
replace C3_1b_6 = "NA" if C3_1b_6=="[8]Skipped" | C3_1b_6=="Don't Know"
replace C3_1b_7 = "NA" if C3_1b_7=="[8]Skipped" | C3_1b_7=="Don't Know"
replace C3_1b_8 = "NA" if C3_1b_8=="[8]Skipped" | C3_1b_8=="Don't Know"

//on immigration issues, get on -1/+1 numeric scales
encode C1_3_a, gen(C13)
recode C13 (8=.)
encode C1_3_b, gen(C13b)
recode C13b (8=.)
encode C1_3_c, gen(C13c)
recode C13c (8=.)
encode C1_3_d, gen(C13d)
recode C13d (8=.)
encode C1_3_e, gen(C13e)
recode C13e (8=.)
encode C1_3_f, gen(C13f)
recode C13f (8=.)
encode C1_3_g, gen(C13g)
recode C13g (8=.)
encode C1_3_h, gen(C13h)
recode C13h (8=.)
encode C1_3_i, gen(C13i)
recode C13i (8=.)
 encode C3_1a , gen(C31)
 recode C31 (1=-1) (2 5 6 =.) (3=1) (4=0)
 encode C3_1b_1 , gen(C31b)
 recode C31b (1=-1) (2 5 6 7=.) (3=1) (4=0)
 encode C3_1b_2 , gen(C31c)
 recode C31c (1=-1) (2 5 6 7=.) (3=1) (4=0)
 encode C3_1b_3 , gen(C31d)
 recode C31d (1=-1) (2 5 6 7=.) (3=1) (4=0)
 encode C3_1b_4 , gen(C31e)
 recode C31e (1=-1) (2 5 6 7=.) (3=1) (4=0)
 encode C3_1b_5 , gen(C31f)
 recode C31f (1=-1) (2 5 6 7=.) (3=1) (4=0)
 encode C3_1b_6 , gen(C31g)
 recode C31g (1=-1) (2 5 6 7=.) (3=1) (4=0)
 encode C3_1b_7 , gen(C31h)
 recode C31h (1=-1) (2 5 6 7=.) (3=1) (4=0)
 encode C3_1b_8 , gen(C31i)
 recode C31i (1=-1) (2 5 6 7=.) (3=1) (4=0)
 replace C13 = (C13-4)/3
 replace C13b = (C13b-4)/3
  replace C13c = (C13c-4)/3
   replace C13d = (C13d-4)/3
    replace C13e = (C13e-4)/3
	 replace C13f = (C13f-4)/3
	  replace C13g = (C13g-4)/3
	   replace C13h = (C13h-4)/3
	   replace C13i = (C13i-4)/3

	   //reverse:  -1 left - +1 right    
	   recode C31 C31b C31c C31d C31e C31f C31g C31h C31i  (1=-1) (-1=1)
	   
//add labels on immigration items
	   foreach var of varlist  C13-C13i {
recode `var' ( -1 /0 = 0 "not leave") (.1/1 =1 "leave"), gen(`var'_01)
}

	   foreach var of varlist  C31-C31i {
recode `var' ( -1 0 = 0 "not decrease") (1 =1 "decrease"), gen(`var'_01)
}
//gen anti-immigration index
gen index= C13_+C31_
gen indexb= C13b_+C31b_
gen indexc= C13c_+C31c_
gen indexd= C13d_+C31d_
gen indexe= C13e_+C31e_
gen indexf= C13f_+C31f_
gen indexg= C13g_+C31g_
gen indexh= C13h_+C31h_
gen indexi= C13i_+C31i_

recode C13_ (.=0),gen(C13nomiss)
recode C31_ (.=0),gen(C31nomiss)
gen index_nomiss= C13no+C31no

//back to original 1 to 7 scale for integration items
 replace C13 = C13*3+4
 replace C13b = C13b*3+4
  replace C13c = C13c*3+4
   replace C13d = C13d*3+4
    replace C13e = C13e*3+4
	 replace C13f = C13f*3+4
	  replace C13g = C13g*3+4
	   replace C13h = C13h*3+4
	   replace C13i = C13i*3+4

//DESCRIPTIVE ANALYSES
//only partisans used to compute parties positions v. partisans of the various parties (closeness in all countries)
//INDEX
//FIG. 1 and TAB B1: average positions of partisans of various parties
eststo clear	   
eststo index_US_dem_sans: mean index  [aw=wei]  if country=="US" & party_10==1 , level(95)
eststo index_US_rep_sans: mean index  [aw=wei]  if country=="US" & party_32==1 , level(95)
eststo index_UK_lab_sans: mean index  [aw=wei]  if country=="UK" & party_16==1 , level(95)
eststo index_UK_con_sans: mean index  [aw=wei]  if country=="UK"  & party_7==1 , level(95)
eststo index_FR_ps_sans: mean index  [aw=wei] if country=="FR"  & party_37==1 , level(95)
eststo index_FR_ump_sans: mean index  [aw=wei]  if country=="FR" & party_41==1 , level(95)
eststo index_IT_pd_sans: mean index  [aw=wei]  if country=="IT"  & party_29==1 , level(95)
eststo index_IT_fi_sans: mean index  [aw=wei]  if country=="IT" & party_13==1 , level(95)
eststo index_GE_spd_sans: mean index  [aw=wei]  if country=="GER"  & party_33==1 , level(95)
eststo index_GE_cdu_sans: mean index  [aw=wei]  if country=="GER"  & party_5==1  , level(95)
eststo index_CA_con_sans: mean index  [aw=wei]  if country=="CAN"  & party_7==1 , level(95)
eststo index_CA_lib_sans: mean index  [aw=wei]  if country=="CAN"  & party_21==1 , level(95)
eststo index_DE_socdem_sans: mean index  [aw=wei]  if country=="DEN"  & party_35==1 , level(95)
eststo index_DE_lib_sans: mean index  [aw=wei] if country=="DEN" & party_22==1 , level(95)
//average party position in eyes of its partisans
eststo index_avg_party_dem: mean indexb [aw=wei] if country=="US" & party_10==1 , level(95)
eststo index_avg_party_rep: mean indexc [aw=wei] if country=="US" & party_32==1 , level(95)
eststo index_avg_party_lab: mean indexc [aw=wei] if country=="UK" & party_16==1 , level(95)
eststo index_avg_party_con: mean indexb [aw=wei] if country=="UK" & party_7==1 , level(95)
eststo index_avg_party_ps: mean indexe [aw=wei] if country=="FR" & party_37==1  , level(95)
eststo index_avg_party_ump: mean indexf [aw=wei] if country=="FR" & party_41==1 , level(95)
eststo index_avg_party_pd: mean indexb [aw=wei] if country=="IT" & party_29==1 , level(95)
eststo index_avg_party_fi: mean indexc [aw=wei] if country=="IT" & party_13==1 , level(95)
eststo index_avg_party_spd: mean indexf [aw=wei] if country=="GER" & party_33==1 , level(95)
eststo index_avg_party_cdu: mean indexb [aw=wei] if country=="GER" & party_5==1  , level(95)
eststo index_avg_party_cancon: mean indexc [aw=wei] if country=="CAN" & party_7==1 , level(95)
eststo index_avg_party_canlib: mean indexd [aw=wei] if country=="CAN" & party_21==1 , level(95)
eststo index_avg_party_A: mean indexh [aw=wei] if country=="DEN"  & party_35==1 , level(95)
eststo index_avg_party_V: mean indexe [aw=wei] if country=="DEN" & party_22==1 , level(95)
esttab using "fig_1.rtf", replace mtitles nostar not
//TAB. 1: mean positions by country
bysort country: tab C13_01  [aw=wei]
bysort country: tab C13_01, m
tab C13_01  [aw=wei]
tab C13_01 ,m 
bysort country: tab  C31_01  [aw=wei]
bysort country: tab  C31_01, m 
tab C31_01  [aw=wei]
tab C31_01 ,m 
bysort country: tab index_no [aw=wei]
tab index_no [aw=wei]
//FIG. B2: integration
//average positions of partisans of various parties
eststo clear	   
eststo C13_US_dem_sans: mean C13  [aw=wei]  if country=="US" & party_10==1 
eststo C13_US_rep_sans: mean C13  [aw=wei]  if country=="US" & party_32==1 
eststo C13_UK_lab_sans: mean C13  [aw=wei]  if country=="UK" & party_16==1 
eststo C13_UK_con_sans: mean C13  [aw=wei]  if country=="UK"  & party_7==1 
eststo C13_FR_ps_sans: mean C13  [aw=wei] if country=="FR"  & party_37==1 
eststo C13_FR_ump_sans: mean C13  [aw=wei]  if country=="FR" & party_41==1 
eststo C13_IT_pd_sans: mean C13  [aw=wei]  if country=="IT"  & party_29==1 
eststo C13_IT_fi_sans: mean C13  [aw=wei]  if country=="IT" & party_13==1 
eststo C13_GE_spd_sans: mean C13  [aw=wei]  if country=="GER"  & party_33==1 
eststo C13_GE_cdu_sans: mean C13  [aw=wei]  if country=="GER"  & party_5==1  
eststo C13_CA_con_sans: mean C13  [aw=wei]  if country=="CAN"  & party_7==1 
eststo C13_CA_lib_sans: mean C13  [aw=wei]  if country=="CAN"  & party_21==1 
eststo C13_DE_socdem_sans: mean C13  [aw=wei]  if country=="DEN"  & party_35==1 
eststo C13_DE_lib_sans: mean C13  [aw=wei] if country=="DEN" & party_22==1 
//average party position in eyes of its partisans
eststo C13_avg_party_dem: mean C13b [aw=wei] if country=="US" & party_10==1 
eststo C13_avg_party_rep: mean C13c [aw=wei] if country=="US" & party_32==1 
eststo C13_avg_party_lab: mean C13c [aw=wei] if country=="UK" & party_16==1
eststo C13_avg_party_con: mean C13b [aw=wei] if country=="UK" & party_7==1
eststo C13_avg_party_ps: mean C13e [aw=wei] if country=="FR" & party_37==1 
eststo C13_avg_party_ump: mean C13f [aw=wei] if country=="FR" & party_41==1 
eststo C13_avg_party_pd: mean C13b [aw=wei] if country=="IT" & party_29==1 
eststo C13_avg_party_fi: mean C13c [aw=wei] if country=="IT" & party_13==1 
eststo C13_avg_party_spd: mean C13f [aw=wei] if country=="GER" & party_33==1 
eststo C13_avg_party_cdu: mean C13b [aw=wei] if country=="GER" & party_5==1  
eststo C13_avg_party_cancon: mean C13c [aw=wei] if country=="CAN" & party_7==1 
eststo C13_avg_party_canlib: mean C13d [aw=wei] if country=="CAN" & party_21==1 
eststo C13_avg_party_A: mean C13h [aw=wei] if country=="DEN"  & party_35==1 
eststo C13_avg_party_V: mean C13e [aw=wei] if country=="DEN" & party_22==1
esttab using "fig_b2.rtf", replace mtitles nostar not
//FIG. B1: level
eststo clear	   
eststo C31_US_dem_sans: mean C31_  [aw=wei]  if country=="US" & party_10==1 
eststo C31_US_rep_sans: mean C31_  [aw=wei]  if country=="US" & party_32==1 
eststo C31_UK_lab_sans: mean C31_  [aw=wei]  if country=="UK" & party_16==1 
eststo C31_UK_con_sans: mean C31_  [aw=wei]  if country=="UK"  & party_7==1 
eststo C31_FR_ps_sans: mean C31_  [aw=wei] if country=="FR"  & party_37==1 
eststo C31_FR_ump_sans: mean C31_  [aw=wei]  if country=="FR" & party_41==1 
eststo C31_IT_pd_sans: mean C31_  [aw=wei]  if country=="IT"  & party_29==1 
eststo C31_IT_fi_sans: mean C31_  [aw=wei]  if country=="IT" & party_13==1 
eststo C31_GE_spd_sans: mean C31_  [aw=wei]  if country=="GER"  & party_33==1 
eststo C31_GE_cdu_sans: mean C31_  [aw=wei]  if country=="GER"  & party_5==1  
eststo C31_CA_con_sans: mean C31_  [aw=wei]  if country=="CAN"  & party_7==1 
eststo C31_CA_lib_sans: mean C31_  [aw=wei]  if country=="CAN"  & party_21==1 
eststo C31_DE_socdem_sans: mean C31_  [aw=wei]  if country=="DEN"  & party_35==1 
eststo C31_DE_lib_sans: mean C31_  [aw=wei] if country=="DEN" & party_22==1 
*average party position in eyes of its partisans
eststo C31_avg_party_dem: mean C31b_ [aw=wei] if country=="US" & party_10==1 
eststo C31_avg_party_rep: mean C31c_ [aw=wei] if country=="US" & party_32==1 
eststo C31_avg_party_lab: mean C31c_ [aw=wei] if country=="UK" & party_16==1
eststo C31_avg_party_con: mean C31b_ [aw=wei] if country=="UK" & party_7==1
eststo C31_avg_party_ps: mean C31e_ [aw=wei] if country=="FR" & party_37==1 
eststo C31_avg_party_ump: mean C31f_ [aw=wei] if country=="FR" & party_41==1 
eststo C31_avg_party_pd: mean C31b_ [aw=wei] if country=="IT" & party_29==1 
eststo C31_avg_party_fi: mean C31c_ [aw=wei] if country=="IT" & party_13==1 
eststo C31_avg_party_spd: mean C31f_ [aw=wei] if country=="GER" & party_33==1 
eststo C31_avg_party_cdu: mean C31b_ [aw=wei] if country=="GER" & party_5==1  
eststo C31_avg_party_cancon: mean C31c_ [aw=wei] if country=="CAN" & party_7==1 
eststo C31_avg_party_canlib: mean C31d_ [aw=wei] if country=="CAN" & party_21==1 
eststo C31_avg_party_A: mean C31h_ [aw=wei] if country=="DEN"  & party_35==1 
eststo C31_avg_party_V: mean C31e_ [aw=wei] if country=="DEN" & party_22==1
esttab using "fig_b1.rtf", replace mtitles nostar not

